package com.gwm.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwm.common.enums.ExceptionEnums;
import com.gwm.common.exceptions.LyException;
import com.gwm.common.threadlocals.UserHolder;
import com.gwm.common.utils.BeanHelper;
import com.gwm.order.entity.TbOrder;
import com.gwm.order.entity.TbOrderDetail;
import com.gwm.order.entity.TbOrderSeckillDetail;
import com.gwm.order.mapper.TbOrderMapper;
import com.gwm.order.service.TbOrderDetailService;
import com.gwm.order.service.TbOrderSeckillDetailService;
import com.gwm.order.service.TbOrderService;
import com.gwm.order.vo.OrderVO;
import com.gwm.seckill.client.SeckillClient;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.gwm.order.enums.OrderStatusEnum.INIT;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author heima
 * @since 2020-12-01
 */
@Service
public class TbOrderServiceImpl extends ServiceImpl<TbOrderMapper, TbOrder> implements TbOrderService {

    @Autowired
    private TbOrderDetailService orderDetailService;

    @Override
    public void updateOverTimeOrderStatus() {
        this.getBaseMapper().updateOverTimeOrderStatus();

    }



    public TbOrder findOrder(Long orderId, String userId, Integer status) {
        QueryWrapper<TbOrder> queryWrapper = new QueryWrapper();
        if(orderId != null){
            queryWrapper.lambda().eq(TbOrder::getOrderId,orderId);
        }
        queryWrapper.lambda().eq(TbOrder::getUserId,Long.parseLong(userId)).
                eq(TbOrder::getStatus,status).
                orderByDesc(TbOrder::getCreateTime);
        TbOrder tbOrder = this.getOne(queryWrapper);
        if(tbOrder == null){
            throw new LyException(ExceptionEnums.ORDER_NOT_FOUND);
        }
        return tbOrder;
    }

    @Override
    public OrderVO findOrderByOrderId(Long orderId) {
        String userId = UserHolder.getUserId();
        Integer status = INIT.value();
        QueryWrapper<TbOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.lambda().eq(TbOrder::getOrderId,orderId)
                .eq(TbOrder::getUserId,Long.parseLong(userId))
                .eq(TbOrder::getStatus,status);
        TbOrder tbOrder = this.getOne(orderQueryWrapper);
        if (tbOrder == null) {
            // 不存在
            throw new LyException(ExceptionEnums.ORDER_NOT_FOUND);
        }
        // 4.封装数据
        OrderVO orderVO = BeanHelper.copyProperties(tbOrder, OrderVO.class);
        return orderVO;
    }

    @Autowired
    private TbOrderSeckillDetailService orderSeckillDetailService;

//    TODO
    @Autowired
    private SeckillClient seckillClient;

   @Override
   public void closeOverTimeSecKillOrder() {
       //        1、查询超时订单对应的订单详情数据
       List<TbOrderSeckillDetail> seckillOrderDetailList = orderSeckillDetailService.findOvertimeSeckillOrderDetail();
       if(seckillOrderDetailList==null||seckillOrderDetailList.size()==0){
           return; //表示没有超时的订单
       }
       Map<Long, Integer> seckillIdAndNumMap = seckillOrderDetailList.stream().collect(Collectors.toMap(TbOrderSeckillDetail::getSeckillId, TbOrderSeckillDetail::getNum));
       //          2、更新超时订单数据
       this.getBaseMapper().cleanOvertimeSeckillOrder();
       //        3、回复库存,别忘了还需要压栈秒杀商品库存数
       seckillClient.plusStock(seckillIdAndNumMap);
   }













}
